package com.wisewin.circle.util.payUtil;

import com.wisewin.circle.util.wechat.config.WXConfig;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import javax.net.ssl.SSLContext;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;

@Deprecated
public class WeChatUtil {
    // 证书信息
    public static final String keystorePath = "/home/cert/wechat/apiclient_cert.p12";
    public static final String keystorePassword = WXConfig.MCHID_JSAPI_HEALTH;


    /**
     * Post请求+证书
     * @param url  微信申请退款接口链接
     * @param entity  微信申请退款接口的参数
     */
    public static HttpResponse httpPost(String url, String entity) {
        System.out.println("======开始调用退款httpPost方法=======");
        try {
            //注意PKCS12证书 是从微信商户平台->账号设置->API安全 中下载的
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            //加载本地的证书进行https加密传输,keystorePath是证书的绝对路径
            FileInputStream instream = new FileInputStream(new File(keystorePath));
            try {
                //设置证书密码,keystorePassword:下载证书时的密码，默认密码是你的mchid
                keyStore.load(instream, keystorePassword.toCharArray());
            } finally {
                instream.close();
            }
            //java 主动信任证书
            //keystorePassword:下载证书时的密码，默认密码是你的mchid
            SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, keystorePassword.toCharArray()).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                    sslcontext, null, null,
                    SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            //CloseableHttpClient 加载证书来访问https网站
            CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            try {
                // 设置响应头信息，发送post请求
                HttpPost httpPost = new HttpPost(url);
                RequestConfig requestConfig = RequestConfig.custom()
                        .setSocketTimeout(5000)
                        .setConnectTimeout(5000)
                        .setConnectionRequestTimeout(5000)
                        .build();
                httpPost.setConfig(requestConfig);
                httpPost.setEntity(new StringEntity(entity, "UTF-8"));
                httpPost.setHeader("Accept", "*/*");
                httpPost.setHeader("Content-type", "application/xml");
                HttpResponse resp = httpClient.execute(httpPost);
                return resp;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 字符串转为对象
     * @param xml  json字符串
     */
    public static Object xmlToPojo(String xml, Class<?> clazz) {
        Object pojo = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller umMarshaller = context.createUnmarshaller();
            pojo = umMarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes("utf-8")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pojo;
    }
}
